﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZkSurvey.BLL.SurveyMgr;
using ZkSurvey.Common.SurveyMgr;
using ZkSurvey.Web.Helper;

namespace ZkSurvey.Web.VP.SurveyMgr
{
    public partial class Detail : System.Web.UI.UserControl
    {
        QuestionBl questionBl = new QuestionBl();
        SurveyBl surveyBl = new SurveyBl();


        protected void Page_Load(object sender, EventArgs e)
        {
            //根据是否传进来参数ExternalUserId 来判断是查看问卷还是查看问卷结果
            if (!IsPostBack)
            {
                int? svId = UrlHelper.GetQueryStringID();
                if (!svId.HasValue)
                {
                    //    ErrorHelper.RaiseNoValueError();
                }
                ViewState["SurveyId"] = svId;


                BindQuestions();
            }

        }

        private void BindQuestions()
        {
            int svId = Convert.ToInt32(ViewState["SurveyId"]);

            List<Question> questions = questionBl.GetQuestionList(svId);
            rptQuestions.DataSource = questions;
            rptQuestions.DataBind();

            Survey survey = surveyBl.GetSurvey(svId);
            ltrQuesCount.Text = questions.Count.ToString();
            ltrSvTitle.Text = survey.Title;
            ltrSvDesciption.Text = survey.Description;
        }

        protected void rptQuestions_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Question question = e.Item.DataItem as Question;
                Literal ltrQueTitle = e.Item.FindControl("ltrQuesTitleFormat") as Literal;
                ltrQueTitle.Text = string.Format("{0}.{1}", question.Sort, question.Title);

                RadioButtonList rblOptions = e.Item.FindControl("rblOptions") as RadioButtonList;
                CheckBoxList cblOptions = e.Item.FindControl("cblOptions") as CheckBoxList;
                TextBox tbxResult = e.Item.FindControl("tbxResult") as TextBox;
                TextBox tbxUserInput = e.Item.FindControl("tbxUserInput") as TextBox;


                if (question.Type == QuestionType.Single)
                {
                    tbxResult.Visible = false;
                    cblOptions.Visible = false;
                    if (!question.AllowInput)
                        tbxUserInput.Visible = false;

                    BindOptions(rblOptions, question);
                }
                else if (question.Type == QuestionType.Multiple)
                {
                    tbxResult.Visible = false;
                    rblOptions.Visible = false;
                    if (!question.AllowInput)
                        tbxUserInput.Visible = false;

                    BindOptions(cblOptions, question);
                }
                else if (question.Type == QuestionType.Input)
                {
                    cblOptions.Visible = false;
                    rblOptions.Visible = false;
                    tbxUserInput.Visible = false;
                }
            }
        }
        private void BindOptions(ListControl lst, Question question)
        {
            lst.DataSource = question.Options;
            lst.DataTextField = "FormatedName";
            lst.DataValueField = "Id";
            lst.DataBind();
        }
    }
}